package cn.app.android.sichuanxiaoke.com.xiaokeapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.AppHomeActivity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.MyAppHomeActivity;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.ConstantUtils;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.OtherUtils;

public class UserLoginToPasswordActivity extends AppCompatActivity {
    @BindView(R.id.tb)
    Toolbar tb;
    @BindView(R.id.phone)
    EditText mphone;
    @BindView(R.id.loginpassword)
    EditText mpassword;
    @BindView(R.id.loginCode)
    EditText mloginCode;
    @BindView(R.id.codeImage)
    ImageView codeiamge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login_to_password);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        setSupportActionBar(tb);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        loadImage();

        codeiamge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadImage();
            }
        });
    }

    private void loadImage(){
        //加载验证码图片  .placeholder(1).error(1)
        Glide.with(this).load(ConstantUtils.USER_LOGIN_PASSWPRD_CODE_URL).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(codeiamge);
    }

    @OnClick(R.id.UserLogin)
    public void OnClick(View view){
        String ph = mphone.getText().toString();
        String pw= mpassword.getText().toString();
        String logcode = mloginCode.getText().toString();
        if(ph.isEmpty()){
            mphone.setError("用户名不能为空");
            return;
        }
        if(pw.isEmpty()){
            mpassword.setError("密码不能为空");
            return;
        }

        boolean phoneNumber = OtherUtils.validatePhoneNumber(ph);
        if(!phoneNumber){
            mphone.setError(getResources().getString(R.string.please_enter_istrue));
            return;
        }
        if(logcode.isEmpty()){
            mloginCode.setError("请输入验证码");
            return;
        }
        SubmitLogin(ph,pw,logcode);

    }

    public void SubmitLogin(String phone,String password,String logcode){


        HttpParams params = new HttpParams();
        double random = Math.random();
        params.put("t",random);
        params.put("username",phone);
        params.put("password",password);
        params.put("verify_code",logcode);

        OkGo.<String>post(ConstantUtils.USER_LOGIN_PASSWPRD_URL).params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                Log.i("logininfo"," password--onSuccess"+body);
                try {
                    JSONObject jsonObject = new JSONObject(body);
                    int status = jsonObject.optInt("status");
                    if(status == 1){
                        Intent intent=new Intent(UserLoginToPasswordActivity.this, MyAppHomeActivity.class);
                        startActivity(intent);

                    }else {
                        Toast.makeText(UserLoginToPasswordActivity.this,"登录失败，请检查登录信息",Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Response<String> response) {
                String body = response.body();
                Log.i("logininfo"," password--onError"+body);
                Toast.makeText(UserLoginToPasswordActivity.this,"访问 出错",Toast.LENGTH_SHORT).show();

            }
        });

    }
}
